package com.gree.changying.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.viewpager.widget.ViewPager;

/**
 * Create by 游德禄 on 2019-11-27
 * 双重嵌套适应外部撑开外部view的高度
 */
public class AutoViewPager extends ViewPager {

    public AutoViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoViewPager(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            if (h > height)
                height = h;
        }
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        requestDisallowInterceptTouchEvent(true);
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE:
//                if(getScrollY() == 0){
//                    requestDisallowInterceptTouchEvent(false);
//                    break;
//                }

                break;
        }
        return super.onTouchEvent(event);
    }

}
